﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Text;
using System.Globalization;
using System.IO;

public partial class Report_JinjiRep : System.Web.UI.Page
{

    DbHelper access = new DbHelper();
    DbCommand cmd = null;
    static DataSet ds = null;
    string strSQL = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        bool isPostBack = base.IsPostBack;
    }

    // Methods
    protected void bind()
    {
        this.strSQL = @"SELECT  TBM.Mat_Name, TDS.Mat_Code as Mat_Code,TDS.StockNo as StockNo,TDS.Num,ISNULL(TDS.Num,0)-ISNULL(TDS.Act_Num,0) as YNUM,
                    TBM.Unit_Base,TDS.Act_Num,
                    TDS.CDT,TBD.OID as OID FROM TDelStorage_List TDS
                    LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TDS.Mat_Code
                    left join TBill_DelStorage TBD on TBD.StockNo = TDS.StockNo
                    WHERE 1 = 1";
        if (txtStockNo.Text != "")
        {
            strSQL = strSQL + "AND TDS.StockNo Like '%" + txtStockNo.Text.Trim() + "%'";
        }
        if (this.txtMat_Code.Text != "")
        {
            this.strSQL = this.strSQL + " AND TDS.Mat_Code LIKE '%" + this.txtMat_Code.Text.ToString().Trim() + "%'";
        }
        if (this.txtMat_Name.Text != "")
        {
            this.strSQL = this.strSQL + " AND TBM.Mat_Name LIKE '%" + this.txtMat_Name.Text.ToString().Trim() + "%'";
        }
        if (this.getDataTime1.BegTime != "")
        {
            this.strSQL = this.strSQL + " AND TDS.CDT >= convert(datetime,'" + this.getDataTime1.BegTime.ToString() + "')";
        }
        if (this.getDataTime1.EndTime != "")
        {
            this.strSQL = this.strSQL + " AND TDS.CDT <=dateadd(d,1,convert(datetime,'" + this.getDataTime1.EndTime.ToString() + "'))";
        }
        //if (this.ddlStatus.SelectedValue == "1")
        //{
        //    this.strSQL = this.strSQL + " AND SUBSTRING(TDS.StockNo,1,2) = 'EB'";
        //}
        //else if (this.ddlStatus.SelectedValue == "2")
        //{
        //    this.strSQL = this.strSQL + " AND SUBSTRING(TDS.StockNo,1,2) != 'EB'";
        //}
        this.strSQL = this.strSQL + " ORDER BY TDS.CDT DESC";
        this.cmd = this.access.GetSqlStringCommond(this.strSQL);
        ds = this.access.ExecuteDataSet(this.cmd);
        this.gv.DataSource = ds.Tables[0].DefaultView;
        this.gv.DataBind();


    }

    protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        //base.Response.Clear();
        //base.Response.BufferOutput = true;
        //base.Response.Charset = "utf-8";
        //DateTime now = DateTime.Now;
        //base.Response.AppendHeader("Content-Disposition", "attachment;filename=" + now.ToString("yyyyMMddHHmmss") + ".xls");
        //base.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
        //base.Response.ContentType = "application/ms-excel";
        //this.EnableViewState = false;
        //CultureInfo formatProvider = new CultureInfo("ZH-CN", true);
        //StringWriter writer = new StringWriter(formatProvider);
        //HtmlTextWriter writer2 = new HtmlTextWriter(writer);
        //this.gv.RenderControl(writer2);
        //base.Response.Write(writer.ToString());
        //base.Response.End();

        if (ds == null || ds.Tables.Count == 0)
        {
            this.strSQL = "SELECT  TBM.Mat_Name, '' + TDS.Mat_Code as Mat_Code,'' + TDS.StockNo as StockNo,TDS.Num,ISNULL(TDS.Num,0)-ISNULL(TDS.Act_Num,0) as YNUM,\r\n                    TBM.Unit_Base,TDS.Act_Num,\r\n                    TDS.CDT,''+ TBD.OID as OID FROM TDelStorage_List TDS \r\n                    LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TDS.Mat_Code \r\n                    left  join TBill_DelStorage TBD on TBD.StockNo=TDS.StockNo\r\n                    WHERE 1=1";
            if (this.txtMat_Code.Text != "")
            {
                this.strSQL = this.strSQL + " AND TDS.Mat_Code LIKE '%" + this.txtMat_Code.Text.ToString().Trim() + "%'";
            }
            if (this.txtMat_Name.Text != "")
            {
                this.strSQL = this.strSQL + " AND TBM.Mat_Name LIKE '%" + this.txtMat_Name.Text.ToString().Trim() + "%'";
            }
            if (this.getDataTime1.BegTime != "")
            {
                this.strSQL = this.strSQL + " AND TDS.CDT >= convert(datetime,'" + this.getDataTime1.BegTime.ToString() + "')";
            }
            if (this.getDataTime1.EndTime != "")
            {
                this.strSQL = this.strSQL + " AND TDS.CDT <=dateadd(d,1,convert(datetime,'" + this.getDataTime1.EndTime.ToString() + "'))";
            }
            if (this.ddlStatus.SelectedValue == "1")
            {
                this.strSQL = this.strSQL + " AND SUBSTRING(TDS.StockNo,1,2) != 'EB'";
            }
            else if (this.ddlStatus.SelectedValue == "2")
            {
                this.strSQL = this.strSQL + " AND SUBSTRING(TDS.StockNo,1,2) = 'EB'";
            }
            this.strSQL = this.strSQL + " ORDER BY TDS.CDT DESC";
            this.cmd = this.access.GetSqlStringCommond(this.strSQL);
            ds = this.access.ExecuteDataSet(this.cmd);
        }
        DataTable dt = ds.Tables[0];
        Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Charset = "GB2312";
        Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("出库报表" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"));
        Response.ContentType = "application/ms-excel";

        Response.Write("出库单号\t物料编码\t物料名称\t单位\t单据数量\t实出数量\t数量差\t操作用户\t操作时间");

        Response.Write(Environment.NewLine);
        foreach (DataRow dr in dt.Rows)
        {

            StringBuilder sb = new StringBuilder();

            sb.Append(dr["StockNo"] + "\t" + dr[1] + "\t" + dr[0] + "\t" + dr["Unit_Base"] + "\t" + dr["Num"] + "\t" + dr["Act_Num"] + "\t" + dr["YNUM"] + "\t" + dr["OID"] + "\t" + dr["CDT"]);

            Response.Write(sb.ToString());
            Response.Write(Environment.NewLine);

        }
        Response.End();
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.bind();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex != -1)
            {
                LinkButton lbtn_StockNo = (LinkButton)e.Row.FindControl("lbtn_StockNo");

                string updateUrl = string.Format("javascript: document.getElementById('{1}').value=window.showModalDialog('RepList.aspx?Rom=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&strStockNo={0}', window, 'dialogWidth=800px;dialogHeight=500px;scroll:no;resizable:no;status:0;');document.URL=location.href;", lbtn_StockNo.CommandArgument.ToString(), hidden.ClientID);
                //string updateUrl = string.Format("javascript: document.getElementById('{2}').value=window.showModalDialog('UnDelDataList.aspx?Rom=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&strStockNo={0}&strAA={1}', window, 'dialogWidth=800px;dialogHeight=500px;scroll:no;resizable:no;status:0;');document.URL=location.href;", lbtn_StockNo.CommandArgument.ToString(), gv.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);


                lbtn_StockNo.Attributes.Add("onclick", updateUrl);

            }

            if (e.Row.RowType == DataControlRowType.DataRow) //判断当前行是不是数据绑定行
            {
                if (Convert.ToDecimal(e.Row.Cells[7].Text.ToString()) > 0)
                //找到库存的索引，设定数量大于0时变颜色
                {
                    e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
                    //设计颜色变化为红色
                }
            }

        }
        catch (Exception ex)
        {

        }

    }
}